package com.tjetc.service.Impl;

import com.tjetc.common.JsonResult;
import com.tjetc.entity.Merchant;
import com.tjetc.service.MerchantAccountService;
import com.tjetc.service.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

//商家端账户业务类
@Service
public class MerchantAccountServiceImpl implements MerchantAccountService {

    @Autowired
    private MerchantService merchantService;

    //使用手机号进行账户登录
    @Override
    public JsonResult login(String phone, String password) {
        Merchant merchant = merchantService.selectByTelephone(phone);
        System.out.println(merchant);

        if (merchant == null || !merchant.getPassword().equals(password)) {

            return new JsonResult<>(1, "账号或者密码错误", null);
        } else {
            return new JsonResult<>(0, "登录成功", merchant);
        }
    }

    //使用手机号进行注册
    @Override
    public JsonResult register(String phone, String password, String name) {
        Merchant merchant = new Merchant();
        merchant.setPassword(password);
        merchant.setSellerTelephone(phone);
        merchant.setSellerName(name);
        merchant.setStatus(-1);

        if (merchantService.addMerchant(merchant) == 0) {
            return new JsonResult<>(1, "手机号重复", null);
        } else {
            return new JsonResult<>(0, "注册成功", merchant);
        }
    }

    //修改密码
    @Override
    public JsonResult modifyPassword(String oldPass, String newPass, Integer merchantId) {
        Merchant merchant = merchantService.selectById(merchantId);
        if (merchant.getPassword().equals(oldPass)) {
            merchant.setPassword(newPass);
            merchantService.updateMerchant(merchant);
            return new JsonResult<>(0, "密码修改成功", null);
        } else {
            return new JsonResult<>(1, "输入的旧密码错误", null);
        }
    }

    //开店或者修改店铺信息
    @Override
    public JsonResult setUp(Merchant merchant,Integer merchantId) {
        Merchant merchant1 = merchantService.selectById(merchantId);
        merchant.setId(merchantId);
        //修改后设置店铺为处于审核状态
        merchant.setStatus(0);
        merchant.setPassword(merchant1.getPassword());
        merchantService.updateMerchant(merchant);
        return new JsonResult(0, "提交成功", null);
    }

    //获得店铺相关的信息
    @Override
    public JsonResult getMerchantMessage(String merchantId) {
        //获取商家信息
        int id = Integer.parseInt(merchantId);
        Merchant merchant = merchantService.selectById(id);
        if (merchant != null) {
            return new JsonResult<>(0, "登录成功", merchant);
        } else {
            return new JsonResult<>(1, "登录状态过期，请重新登录", null);
        }
    }

    @Override
    public JsonResult changeShopStatus(Integer merchantId, Integer status) {
        Merchant merchant=merchantService.selectById(merchantId);
        merchant.setStatus(status);
        return new JsonResult(0,"修改状态成功",merchantService.updateMerchant(merchant));

    }

    @Override
    public JsonResult changeDropFee(Integer merchantId, BigDecimal dropFee) {
        Merchant merchant=merchantService.selectById(merchantId);
        merchant.setDropOffFee(dropFee);
        return new JsonResult(0,"修改起送费",merchantService.updateMerchant(merchant));
    }
}
